package com.leejw.tradeblogsystem.controller;

import com.leejw.tradeblogsystem.common.Result;
import com.leejw.tradeblogsystem.model.Article;
import com.leejw.tradeblogsystem.model.Record;
import com.leejw.tradeblogsystem.model.Shares;
import com.leejw.tradeblogsystem.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author: jwli3
 * @Desc:
 * @create: 2024-08-27
 **/
@Slf4j
@RestController
@RequestMapping("/record")
public class RecordController {
    Logger logger = LoggerFactory.getLogger(RecordController.class);

    @Autowired
    private RecordService recordService;

    /**
     * 保存记录
     * @param recordVO
     * @return
     */
    @ResponseBody
    @RequestMapping("/saveRecord")
    public Result saveRecord(@RequestBody Record recordVO) {
        return recordService.saveRecord(recordVO);
    }

    /**
     * 查询操作记录表
     * */
    @ResponseBody
    @RequestMapping("/getAllRecords")
    public List<Record> getAllRecords() {
        log.info("start!开始查询……");
        List<Record> all = recordService.getAllRecords();
        log.info("end!查询结束……");
        return all;
    }

    /**
     * 实现操作记录批量导入
     * */
    @RequestMapping(value = "/importRecords", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result importShareExcel(@RequestParam MultipartFile excelFile) throws Exception {
        //获取上传的文件
        logger.info("开始导入数据...");
        Result result = new Result();
        Record record = new Record();
        try {
            Integer num = recordService.importRecordExcel(excelFile,record);
            result.setCode(num);
            result.setMsg("导入成功！");
        } catch (Exception e) {
            result.setCode(500);
            return Result.error("导入失败!");
        }
        return result;
    }
}
